package com.kauto.gps.bean;

import lombok.Data;

@Data
public class HttpResult<T> {
    boolean success;
    int code;
    String msg;
    T data;

    public static HttpResult success() {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.SUCCESS.success);
        result.setCode(Status.SUCCESS.code);
        result.setMsg(Status.SUCCESS.msg);
        return result;
    }

    public static HttpResult success(Object data) {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.SUCCESS.success);
        result.setCode(Status.SUCCESS.code);
        result.setMsg(Status.SUCCESS.msg);
        result.setData(data);
        return result;
    }

    public static HttpResult failed() {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.ERROR.success);
        result.setCode(Status.ERROR.code);
        result.setMsg(Status.ERROR.msg);
        return result;
    }

    public static HttpResult failed(String msg) {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.OTHERS_ERROR.success);
        result.setCode(Status.OTHERS_ERROR.code);
        result.setMsg(msg);
        return result;
    }

    public static HttpResult failed(int code, String msg) {
        HttpResult result = new HttpResult();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static HttpResult failed(Status status) {
        HttpResult result = new HttpResult();
        result.setSuccess(false);
        result.setCode(status.code);
        result.setMsg(status.msg);
        return result;
    }

    public static HttpResult requiredParamBlank(String resMsg) {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.REQUIRED_PARAM_BLANK.success);
        result.setCode(Status.REQUIRED_PARAM_BLANK.code);
        result.setMsg(resMsg);
        return result;
    }

    public enum Status {
        SUCCESS(true, 200, "操作成功"),
        REQUIRED_PARAM_BLANK(false, 301, "必填参数为空"),
        ERROR(false, 302, "系统异常"),
        TOKEN_IS_EXPIRED(false, 303, "用户token已过期,请重新登录!"),
        OTHERS_ERROR(false, 999, "");


        public final boolean success;
        public final int code;
        public final String msg;

        Status(boolean success, int code, String msg) {
            this.success = success;
            this.code = code;
            this.msg = msg;
        }
    }

}
